<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

require_once COORDINATOR.DS."head.php";
?>
<script type='text/javascript'>
$(document).ready(function(){
	$("#search").keyup(function(){
		$(".thead").show();
		$(".reports .email").each(function(i){
			
			if ($(this).html().search($("#search").val()) == -1)
				$(this).parent().hide();
			else
				$(this).parent().show();
		});
		$("tr:not(.thead):not(.report_head)").each(function(i,a){
			if ($(this).css('display') == 'none')
				$(this).prev(".thead").hide();
		});
	})
})
</script>
<div class='search'>
	Поиск по пользователю: <input type='text' id='search' /><br /><br />
</div>
<?php
$query = "select `R`.`report_id`,`R`.`user_id`,`R`.`date` as `r_date`,`C`.`name` as `c_name`,`A`.`name` as `a_name`,`J`.* from `joined` as `J`
left join `reports` as `R` on  `R`.`report_id` = `J`.`report_id`
left join `cities` as `C` on `J`.`city`=`C`.`city_id`
left join `areas` as `A` on `J`.`area`=`A`.`area_id`
where `R`.`user_id` = '".$user->getUserId()."'
order by `r_date` desc";
$reports = $db->getTable($query);
$rep = 0;
echo "<table class='reports'>";
echo "<tr class='report_head'><td>Пользователь</td><td>Дата</td><td>Пол</td><td>Воз.</td><td>Адрес</td><td class='comment_td'>Комментарий</td><td>Действия</td></tr>";
foreach ($reports as $report)
{
	if ($rep != $report['report_id'])
	{
		echo "<tr class='thead'><td colspan='12'>Отчет от ".date("d.m.Y H:i",$report['r_date'])."</td></tr>";
		$rep = $report['report_id'];
	}
	echo "<tr>";
	echo "<td class='email'>$report[email]<br />($report[sname] $report[name] $report[tname])</td>";
	echo "<td>".date("d.m.Y H:i",$report['date'])."</td>";
	echo "<td>".($report['sex'] == 'male' ? "Муж." : ($report['sex'] == 'female' ? "Жен." : "Неиз.")) ."</td>";
	echo "<td style='width:60px'>".($report['age'] != 0 ? wordEnding($report['age'],"год","года","лет") : "неиз.")."</td>";
	echo "<td style='width:160px;'>$report[c_name]<br />(".($report['a_name'] ? $report['a_name']." р-н" : "Не известно р-н").")<br />$report[street]</td>";
	echo "<td align='center'><div class='comment ".($report['comment'] == "" ? "red" : "green")."'></div></td>";
	echo "<td><a class='send right' href='/coordinator/reports/edit/joined/".$report['joined_id']."'>Редактировать</a><br /><a class='delete right' href='/coordinator/reports/delete/joined/".$report['joined_id']."'>Удалить</a></td>";
	echo "</tr>";
}
echo "</table>";

require_once COORDINATOR.DS."footer.php";
?>